package com.lcredu.springboot.comtroller;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 黎长容
 * @date 2025-08-11 18:32
 */
@RestController
//@Controller
public class ParameterController {

    @GetMapping("/monster/{id}/{name}")
    public String pathVariable(@PathVariable("id") Integer id,
                               @PathVariable("name") String name,
                               @PathVariable Map<String, String> map) {
        System.out.println("id=" + id);
        System.out.println("name=" + name);
        System.out.println("map=" + map);
        return "success";
    }


    @RequestMapping("/requestHeader")
    public String Request(@RequestHeader("host") String host,
                          @RequestHeader("Accept") String accept,
                          @RequestHeader Map<String, String> map) {
        System.out.println("host=" + host);
        System.out.println("accept=" + accept);
        System.out.println("map=" + map);
        return "success";
    }


    @RequestMapping("/hi")
    public String hi(@RequestParam("name") String username,
                     @RequestParam("fruit") List<String> fruits,
                     @RequestParam Map<String, String> map) {
        System.out.println("name=" + username);
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
        System.out.println(map);
        return "success";
    }

    @GetMapping("/cookie")
    public String cookie(@CookieValue(value = "cookie_lcr", required = false) String cookie_name,
                         @CookieValue(value = "cookie_must", required = false) Cookie cookie_must,
                         HttpServletRequest request) {
        System.out.println("cookie_lcr=" + cookie_name);
        System.out.println("cookie_must=" + cookie_must);
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println("cookieName=" + cookie.getName());
            System.out.println("cookieValue=" + cookie.getValue());
        }
        return "success";
    }

    @PostMapping("/save")
    public String postMethod(@RequestBody String content) {
        System.out.println("content= " + content);
        return "success";
    }


}
